package com.cakebackoffice;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.cakebackoffice.Model.Store;

import java.util.ArrayList;
import java.util.List;


public class AdminStoreListFragment extends Fragment {

    private LinearLayout mListLayout;
    private List<Store> storeList = new ArrayList<Store>();
    private ListView listView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mListLayout = (LinearLayout) inflater.inflate(R.layout.admin_store_list_fragment, container, false);
        storeList = Store.getAll(getActivity().getBaseContext());
        return mListLayout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.refresh();
        Button button = (Button) getActivity().findViewById(R.id.buttonAddStore);
        button.setOnClickListener(onButtonAddClicked);
        this.listView.setOnItemClickListener(lvListenner);
    }

    public void refresh() {
        this.listView = (ListView) getActivity().findViewById(R.id.ListView_store_list);
        ListAdapter adapter = new ListViewStoreAdapter(getActivity().getBaseContext(), R.layout.file_row, storeList);
        this.listView.setAdapter(adapter);
    }

    // Listenners

    // Afficher un store:
    private ListView.OnItemClickListener lvListenner = new ListView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            // On récupére l'objet sélectionné
            Store selectedStore = (Store) adapterView.getItemAtPosition(i);

            ((AdminStoreActivity) getActivity()).onStoreSelected(selectedStore);

        }
    };

    // Ajouter un store:
    private View.OnClickListener onButtonAddClicked = new View.OnClickListener() {
        public void onClick(View v) {
            ((AdminStoreActivity) getActivity()).openDialogAddStore();
        }
    };


}
